<?php
// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------

namespace app\customer\controller;
use think\helper\Hash;

/**
 * 客服登录
 * @package app\user\controller
 */
class Login extends \think\Controller
{
    
    protected  $user_id = null;
    protected function initialize()
    {          
      
    }
    /**
     * 登录界面
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

     /**
     * 隐私协议
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function privacy(){
       $info = \app\operation\model\Article::getOne(2);
       $this->assign("info",$info);
       return $this->fetch("article");
    }
     /**
     * 服务协议
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function service(){
       $info = \app\operation\model\Article::getOne(1);
       $this->assign("info",$info);
       return $this->fetch("article");
    }
     /**
     * 登录操作
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function login(){
        $param = $this->request->post();
        if(empty($param["mobile"])){
            $this->error("手机号不能为空");
        }
        if(empty($param["password"])){
            $this->error("密码不能为空");
        }
        if(empty($param["code"])){
            $this->error("验证码不能为空");
        }
        foreach(["privacy","service","sexy","civilized","safe"] as $name){
            if(empty($param["protocol"][$name])){
                $this->error("请同意相关协议、公约和法规");
            }
        }
        //验证验证码 
        if(!captcha_check($param['code'],"service")){
                $this->error('验证码错误');
        }
                // 查找用户
        $user = \app\user\model\User::where("mobile",$param["mobile"])->where("status",1)->find();
        if (!$user) {
            $this->error("账号或者密码错误");
        }
        if (!Hash::check((string)$param["password"], $user['password'])) {
            $this->error("账号或者密码错误");
        }
        
        $list = \app\customer\model\Customer::where("member_id",$user["id"])->count();
        if($list < 1){
            $this->success("登录中，请稍后",url('noservice'));
        }
        session("service_user_id", $user['id']);
        $this->success("登录成功",url('/customer/message/index'));       
    }
      /**
     * 登录失败
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function noservice(){
        return $this->fetch();
    }
    /**
     * 登出
     * @return mixed
     * @author php迷途小书童
     * @created 2020/12/18 20:55
     */
    public function signout(){
        session("service_user_id", 0);
        $this->redirect(url("/customer/login/index"));
    }
    
     /**
     * 移动端自动登录
     * @author 晓风<215628355@qq.com>
     * @return mixed
     */
    public function mlogin()
    {
        $param = input("get.");
        $token = $param["token"] ?? null;
        if(!$token){
            return $this->fetch("error");
        }
        if (!preg_match("/^[a-fA-F0-9]{32}$/", $token)) {
            return $this->fetch("error");
        }        
        $redis = \app\common\model\Redis::handler();
        $info = $redis->get('ST_'.$token);
     
        if(!$info){
            return $this->fetch("error");
        }
        $info = json_decode($info,true);
        if(!is_array($info)){
            return $this->fetch("error");
        }        
        if(empty($info["user_id"])){
            return $this->fetch("error");
        }    
        session("service_user_id", $info['user_id']);
        $url = url('/customer/message/index');
        $this->assign('url',$url);
        return $this->fetch();  
    }
}